﻿
@{
    var Username = "";
    var isAdmin = "False";
    var isDeleted = "False";
    try{
        Username = Session["u.Username"].ToString();
        isAdmin = Session["u.isAdmin"].ToString();
    }catch{
        Username = "";
        isAdmin = "";
    }

    var UserId = "";
    try{
        UserId = Session["u.UserId"].ToString();
       } catch{
            UserId = "";
       }
  
    var db = Database.Open("CvProjectDatabase");
    var selectQuery = "Select * from Users Where Username like @0";
    var user = db.QuerySingle(selectQuery, UserId);
    var selectApp = "Select A.AppId,FirstName,LastName,CreationDate,PracticalScore,TheoreticalScore,Name,A.isDeleted From Applicants A, HistoryEntry B, Tags C Where A.isDeleted = @0 and A.AppId = B.AppId and A.AppId = C.AppId";        
    var selectHistoryEntry = "SELECT * FROM HistoryEntry WHERE IsDeleted = 'False'";       
    var addQuery = "INSERT INTO HistoryEntry (CreationDate, Comment, IsDeleted, HTypeId, AppId, UserId) VALUES (@0, @1, @2, @3, @4, @5)";
    var updateQuery = "UPDATE HistoryEntry SET CreationDate = @0, Comment = @1, IsDeleted = @2, HTypeId = @3, AppId = @4, UserId = @5 WHERE HId LIKE @6";

    var inputEntryDate = "";       
    var inputComments = "";
    var IsDeleted = "False";
    
    // select entry type
    var selectHistoryEntryType = "SELECT * FROM HistoryEntryType WHERE IsDeleted = 'False'";
    var entryType = db.Query(selectHistoryEntryType);
    var et = Request.Form["entType"];
    var idEntType = 0;
    foreach (var eType in entryType){
        if(et == eType.Name){
            idEntType = eType.HTypeId;
        }
    }
       var AppId = Request.QueryString["AppId"];
       var inputHId = Request.QueryString["HId"];

        if(IsPost){            
            Validation.RequireField("comments", "You have to enter comments!");
            var edit = Request.QueryString["edit"].AsBool();
                if(Validation.IsValid() && edit){
                    inputEntryDate = Request.Form["entryDate"];
                    inputComments = Request.Form["comments"];                   
                    db.Execute(updateQuery, inputEntryDate, inputComments, IsDeleted, idEntType, AppId, UserId, inputHId);
                    Response.Redirect("Index.cshtml");
                }
        if(Validation.IsValid()){
                inputEntryDate = Request.Form["entryDate"];
                inputComments = Request.Form["comments"];
                db.Execute(addQuery, inputEntryDate, inputComments, IsDeleted, idEntType, AppId, UserId);
                Response.Redirect("Index.cshtml");
            }
         }

}


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="res/css/bootstrap.min.css" rel="stylesheet">

   

  </head>
  <body>
       @if(Username.IsEmpty() || Username == ""){
            Response.Redirect("Login.cshtml"); 
      }

       <nav class="navbar navbar-default" role="navigation">
            <div class="container-fluid">
                 <ul id="navigacija" class="nav navbar-nav">
                     <li style="margin-right: 30px"><a href="Index.cshtml"><b style="font-size: 1.7em">CV Database</b></a></li>
                     <li style="font-size: 1.2em"><a href="UserAdministration.cshtml">User Administration</a></li>
                     <li style="padding-top: 15px"> | </li>
                   
                    
                        <li class="dropdown" style="font-size: 1.2em">
                        
                            
                            <li style="font-size: 1.2em"><a href="index.cshtml?izlogujSe=true">Logout</a></li>
                        </ul>
                    </li>
                 </ul>
            </div>
        </nav>
        <form method="post">
             <p><label for="entryDate">Entry date:</label>
                  <input type="date" name="entryDate"   @Request.QueryString["entryDate"]/>
             </p>           
             <p><label for="entType">Entry type</label>
                    <select name="entType">
                        @foreach(var eType in entryType){
                           <option id="@eType.HTypeId">@eType.Name</option>
                        } 
                    </select>
             </p>
             <p><label for="comments">Comments:</label></p>
                      <textarea name="comments" rows="4" cols="29"  @Validation.For("comments") >@Request.QueryString["comment"]</textarea >
             <p>
                 <p class="validacija">
                 @Html.ValidationMessage("comments")
                 </p>
             </p>
             <p><input class="compBot"type="submit" name="buttonSubmit" value="Save" /></p>
            </form>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="res/js/bootstrap.min.js"></script>
  </body>
</html>